//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.constant.e.BaseEnum;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.basic.biz.IModelBiz;
import net.mingsoft.basic.biz.IRoleModelBiz;
import net.mingsoft.basic.constant.e.ModelIsMenuEnum;
import net.mingsoft.basic.dao.IModelDao;
import net.mingsoft.basic.entity.ModelEntity;
import net.mingsoft.basic.entity.RoleModelEntity;
import net.mingsoft.basic.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("modelBiz")
@Transactional
public class ModelBizImpl extends BaseBizImpl<IModelDao, ModelEntity> implements IModelBiz {
    private IModelDao modelDao;
    @Autowired
    private IRoleModelBiz roleModelBiz;

    public ModelBizImpl() {
    }

    public ModelEntity getEntityByModelCode(BaseEnum modelCode) {
        return this.modelDao.getEntityByModelCode(modelCode.toString());
    }

    public ModelEntity getEntityByModelCode(String modelCode) {
        return this.modelDao.getEntityByModelCode(modelCode);
    }

    public IModelDao getModelDao() {
        return this.modelDao;
    }

    @Autowired
    public void setModelDao(IModelDao modelDao) {
        this.modelDao = modelDao;
    }

    protected IBaseDao getDao() {
        return this.modelDao;
    }

    public ModelEntity getModel(String modelType, int modelId) {
        return this.modelDao.getModel(modelType, modelId);
    }

    public List<ModelEntity> queryModelByRoleId(int roleId) {
        return this.modelDao.queryModelByRoleId(roleId);
    }

    /** @deprecated */
    @Deprecated
    @Transactional(
            rollbackFor = {Exception.class, Error.class}
    )
    public void reModel(ModelEntity modelParent, String parentIds, int mangerRoleId, List<RoleModelEntity> roleModels, Integer parentId) {
        modelParent.setModelIsMenu(ObjectUtil.isNotNull(modelParent.getModelChildList()) && modelParent.getModelChildList().size() > 0 ? 1 : 0);
        modelParent.setModelId(parentId);
        modelParent.setModelDatetime(new Timestamp(System.currentTimeMillis()));
        modelParent.setModelParentIds(parentIds);
        ModelEntity modelParentEntity = this.getEntityByModelCode(modelParent.getModelCode());
        if (modelParentEntity == null) {
            if (modelParent.getModelIsMenu() == ModelIsMenuEnum.MODEL_MEUN.toInt()) {
                ModelEntity modelEntity = new ModelEntity();
                modelEntity.setModelIsMenu(ModelIsMenuEnum.MODEL_MEUN.toInt());
                modelEntity.setModelTitle(modelParent.getModelTitle());
                modelEntity.setModelId(modelParent.getModelId());
                if (ObjectUtil.isNotEmpty(this.getEntity(modelEntity))) {
                    throw new BusinessException("菜单标题重复");
                }
            }

            this.saveEntity(modelParent);
            RoleModelEntity roleModel = new RoleModelEntity();
            roleModel.setRoleId(mangerRoleId);
            roleModel.setModelId(Integer.parseInt(modelParent.getId()));
            roleModels.add(roleModel);
        } else {
            modelParent.setId(modelParentEntity.getId());
            this.updateEntity(modelParent);
        }

        if (ObjectUtil.isNotNull(modelParent.getModelChildList()) && modelParent.getModelChildList().size() > 0) {
            Iterator var10 = modelParent.getModelChildList().iterator();

            while(var10.hasNext()) {
                ModelEntity modelEntity = (ModelEntity)var10.next();
                this.reModel(modelEntity, StringUtils.isBlank(parentIds) ? modelParent.getId() : parentIds + "," + modelParent.getId(), mangerRoleId, roleModels, Integer.parseInt(modelParent.getId()));
            }
        }

    }

    /** @deprecated */
    @Deprecated
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void jsonToModel(String menuStr, int mangerRoleId, int modelId) {
        List<RoleModelEntity> roleModels = new ArrayList();
        if (StringUtils.isNotBlank(menuStr)) {
            List<ModelEntity> list = JSONUtil.toList(menuStr, ModelEntity.class);

            ModelEntity modelParent;
            for(Iterator var6 = list.iterator(); var6.hasNext(); this.reModel(modelParent, (String)null, mangerRoleId, roleModels, modelId)) {
                modelParent = (ModelEntity)var6.next();
                ModelEntity entity = (ModelEntity)this.getEntity(modelParent);
                if (entity != null && (entity.getModelId() == null || entity.getModelId() == 0)) {
                    this.deleteEntity(Integer.parseInt(entity.getId()));
                }
            }

            if (roleModels.size() > 0) {
                this.roleModelBiz.saveBatch(roleModels, roleModels.size());
            }
        }

    }

    @Transactional(
            rollbackFor = {Exception.class, Error.class}
    )
    public void importModel(ModelEntity modelEntity, int mangerRoleId, String parentIds, Integer modelId) {
        List<RoleModelEntity> roleModels = new ArrayList();
        modelEntity.setId((String)null);
        modelEntity.setModelDatetime(new Timestamp(System.currentTimeMillis()));
        if (modelId != null && modelId != 0) {
            modelEntity.setModelId(modelId);
            modelEntity.setModelParentIds(parentIds);
        } else {
            modelEntity.setModelId((Integer)null);
            modelEntity.setModelParentIds((String)null);
        }

        this.modelDao.insert(modelEntity);
        RoleModelEntity roleModel = new RoleModelEntity();
        roleModel.setRoleId(mangerRoleId);
        roleModel.setModelId(Integer.parseInt(modelEntity.getId()));
        roleModels.add(roleModel);
        if (roleModels.size() > 0) {
            this.roleModelBiz.saveBatch(roleModels, roleModels.size());
        }

        ModelEntity curModelEntity;
        if (ObjectUtil.isNotNull(modelEntity.getModelChildList()) && modelEntity.getModelChildList().size() > 0) {
            for(Iterator var7 = modelEntity.getModelChildList().iterator(); var7.hasNext(); this.importModel(curModelEntity, mangerRoleId, parentIds, Integer.parseInt(modelEntity.getId()))) {
                curModelEntity = (ModelEntity)var7.next();
                if (!StringUtils.isBlank(parentIds) && modelId != null && modelId != 0) {
                    parentIds = modelId + "," + modelEntity.getId();
                } else {
                    parentIds = modelEntity.getId();
                }
            }
        }

    }

    public void updateEntity(ModelEntity model) {
        this.setParentId(model);
        this.modelDao.updateById(model);
        this.setChildParentId(model);
        this.modelDao.updateCache();
    }

    public void saveEntity(ModelEntity model) {
        this.setParentId(model);
        this.modelDao.insert(model);
        this.modelDao.updateCache();
    }

    public List<ModelEntity> queryChildList(ModelEntity modelEntity) {
        ModelEntity model = (ModelEntity)this.modelDao.selectOne(new QueryWrapper(modelEntity));
        if (model == null) {
            return null;
        } else {
            ModelEntity _model = new ModelEntity();
            _model.setModelId(model.getIntId());
            List<ModelEntity> list = this.modelDao.selectList(new QueryWrapper(_model));
            return list;
        }
    }

    private void setParentId(ModelEntity model) {
        if (model.getModelId() != null && model.getModelId() > 0) {
            ModelEntity _model = (ModelEntity)this.modelDao.selectById(model.getModelId());
            if (StringUtils.isEmpty(_model.getModelParentIds())) {
                model.setModelParentIds(_model.getId());
            } else {
                model.setModelParentIds(_model.getModelParentIds() + "," + _model.getId());
            }
        } else {
            model.setModelParentIds((String)null);
            model.setModelId((Integer)null);
        }

    }

    private void setChildParentId(ModelEntity model) {
        ModelEntity _model = new ModelEntity();
        _model.setModelId(Integer.parseInt(model.getId()));
        List<ModelEntity> list = this.modelDao.query(_model);
        list.forEach((x) -> {
            if (StringUtils.isEmpty(model.getModelParentIds())) {
                x.setModelParentIds(model.getId());
            } else {
                x.setModelParentIds(model.getModelParentIds() + "," + model.getId());
            }

            super.updateEntity(x);
            this.setChildParentId(x);
        });
    }
}
